//
//  RSOriginalView.swift
//  RSWeibo
//
//  Created by Macx on 16/8/12.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

// 控件跟边界的差距, 以及部分控件之间距离
let statusSubViewMargin: CGFloat = 10

class RSOriginalView: UIView {
    
    // 用来记录底部约束的
    var bottomConstraint: Constraint?
    
    
    /*
     拥有一个存储网络请求回来的数据的模型
     */
    var statusViewModel: RSStatusViewModel?{
        didSet{
            
            // 设置头像数据
            if let profile = statusViewModel?.statusModel?.user?.profile_image_url {
            
                headImgView.sd_setImageWithURL(NSURL(string: profile), placeholderImage: UIImage(named: "avatar_default_big"))
                
            }
            
            // 设置昵称
            nameLab.text = statusViewModel?.statusModel?.user?.screen_name
            // 微博正文
            contentLab.text = statusViewModel?.statusModel?.text
            // 微博来源
            sourceLab.text = statusViewModel?.sourceContent
            // 微博等级视图
            menberRankImgView.image = statusViewModel?.mbRankImg
            // 认证类型
            certifyRankImgView.image = statusViewModel?.verifiedTypeImg
            
            // 发布时间
            timeLab.text = statusViewModel?.creatTimeContent
            
            
            // 给 配图视图 添加数据源方法
            picturesView.picUrls = statusViewModel?.statusModel?.pic_urls
            
            //              卸载上一次约束
            bottomConstraint?.uninstall()
            
            //            print("总共的转发配图\(retweetViewModel?.statusModel?.retweeted_status?.pic_urls)")
            
            //  判断配图是否存在并且是否大于0
            if let picUrls = statusViewModel?.statusModel?.pic_urls where picUrls.count > 0 {
                //  显示配图
                picturesView.hidden = false
                //  设置配图信息
                picturesView.picUrls = picUrls
                //  更新约束
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint =  make.bottom.equalTo(picturesView).offset(statusSubViewMargin).constraint
                })
                
                
            } else {
                //  隐藏配图
                picturesView.hidden = true
                
                //  更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLab).offset(statusSubViewMargin).constraint
                })
            }

            
        
        
        }
        
    }
    
    
    
    
    // 所拥有的视图
    
    // 头像
    private lazy var headImgView = UIImageView(image: UIImage(named: "avatar_default_big"))
    // 认证等级类型图片
    private lazy var certifyRankImgView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 昵称  使用拓展中 重新写的 便利构造方法
    private lazy var nameLab = UILabel(fontSize: 15, textColor: UIColor.darkGrayColor())
    // 会员等级
    private lazy var menberRankImgView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 时间
    private lazy var timeLab = UILabel(fontSize: 12, textColor: UIColor.orangeColor())
    // 来源
    private lazy var sourceLab = UILabel(fontSize: 12, textColor: UIColor.lightGrayColor())
    // 微博正文
    private lazy var contentLab: UILabel = {
    
      var lable =  UILabel(fontSize: 14, textColor: UIColor.darkGrayColor())
        
        // 多行显示
        lable.numberOfLines = 0
        
        return lable
        
    }()
    
    // 原创微博的正文配图框
    private lazy var picturesView: RSPictureView = RSPictureView()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /*
         测试显示内容
         */
        nameLab.text = "我就是我啊"
        contentLab.text = "shfuaas g是奇偶发噶啥给教官好 加过啊发送结果加上个 噶啥个十多个为奥规划法规 估计是高级给更关键时刻v安居偶睡觉撒娇歌 纪律及覅及vv奥覅偶分 就熊结果爱国供热个人让给供热u如果热耦合就"
        timeLab.text = "刚刚"
        
        self.backgroundColor = UIColor(white: 0.99, alpha: 1)
        
        setOriginalView()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setOriginalView(){
    
        // 添加到父控件中
        addSubview(headImgView)
        addSubview(certifyRankImgView)
        addSubview(nameLab)
        addSubview(menberRankImgView)
        addSubview(timeLab)
        addSubview(sourceLab)
        addSubview(contentLab)
        addSubview(picturesView)
        
        // 设置约束
        headImgView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(statusSubViewMargin)
            make.leading.equalTo(self).offset(statusSubViewMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        certifyRankImgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImgView.snp_trailing)
            make.centerY.equalTo(headImgView.snp_bottom)
        }
        
        nameLab.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImgView.snp_trailing).offset(statusSubViewMargin)
            make.top.equalTo(headImgView)
        }
        
        menberRankImgView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLab.snp_trailing).offset(statusSubViewMargin)
            make.top.equalTo(nameLab)
        }
        
        timeLab.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLab)
            make.bottom.equalTo(headImgView)
        }
        
        sourceLab.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImgView)
            make.leading.equalTo(timeLab.snp_trailing).offset(statusSubViewMargin)
        }
        
        contentLab.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImgView)
            make.top.equalTo(headImgView.snp_bottom).offset(statusSubViewMargin)
            make.trailing.equalTo(self).offset(-statusSubViewMargin)
        }
        picturesView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLab.snp_bottom).offset(statusSubViewMargin)
            make.leading.equalTo(contentLab)
            make.trailing.equalTo(contentLab)
            
        }

        // 原创视图的底部约束 = 微博正文的底部约束 + 边距
        self.snp_makeConstraints { (make) in
             make.bottom.equalTo(contentLab.snp_bottom).offset(statusSubViewMargin).constraint
        }
    
    
    }
    
}
